﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using UnitOfWorkCore.Domain.ToolKits.Extensions;

namespace UnitOfWorkCore.Domain.Abstractions
{

    /// <summary>
    /// 主仓储接口（默认主键类型Guid）
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public interface IRepository<TEntity> : IRepository<TEntity, Guid>
       where TEntity : Entity<Guid>, IAggregateRoot
    {

    }

    /// <summary>
    /// 通用仓储接口（保留泛型主键版本）
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="TKey"></typeparam>
    public interface IRepository<TEntity, TKey> where TEntity : Entity<TKey>, IAggregateRoot where TKey : notnull
    {
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        Task<bool> DeleteArray(List<TKey> array);

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        Task<bool> InsertManyData(List<TEntity> array);

        /// <summary>
        /// 根据id查询单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<TEntity> QuerySingleData(TKey id);

        /// <summary>
        /// 查询单条数据
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<TEntity> QuerySingleData(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 查询单条数据
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<TEntity> QuerySingleData(Expression<Func<TEntity, bool>> expression, Func<TEntity, dynamic> sort);

        /// <summary>
        /// 查询单条数据
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<TEntity> QuerySingleData(Expression<Func<TEntity, bool>> expression, IEnumerable<Func<TEntity, dynamic>> sorts, string sortType);

        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <returns></returns>
        Task<bool> DeleteSingleData();

        /// <summary>
        /// 修改单条数据
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        Task UpdateSingleData(TEntity data);

        /// <summary>
        /// 修改多条数据
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        Task UpdateManyData(List<TEntity> array);

        /// <summary>
        /// 添加单条数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        Task InsertSingleData(TEntity data);

        /// <summary>
        /// 添加/修改单条数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        Task<Tuple<bool, int>> InsertOrUpdateSingleData(TEntity data);

        /// <summary>
        /// 单表/视图分页查询
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<Tuple<List<TEntity>, int>> QueryPageData(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 单表/视图分页查询
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        Task<Tuple<List<TEntity>, int>> QueryPageData(Expression<Func<TEntity, bool>> expression, string sort, string order);

        /// <summary>
        /// 单表/视图分页查询
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="sort"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        Task<Tuple<List<TEntity>, int>> QueryPageData(Expression<Func<TEntity, bool>> expression, Func<TEntity, dynamic> sort, string sortType);

        /// <summary>
        /// 单表/视图分页查询
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="sorts"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        Task<Tuple<List<TEntity>, int>> QueryPageData(Expression<Func<TEntity, bool>> expression, IEnumerable<Func<TEntity, dynamic>> sorts, string sortType);

        /// <summary>
        /// 单表/视图查询=>返回指定对象
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        Task<IPageInfo<dynamic>> QueryPageData(Expression<Func<TEntity, bool>> expression, Func<TEntity, dynamic> sort);

        /// <summary>
        /// 根据条件查询集合
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<List<TEntity>> QueryTableList(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 根据条件查询集合=>特定对象集合
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="expression"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        Task<List<TResult>> QueryTableList<TResult>(Expression<Func<TEntity, bool>> expression, Expression<Func<TEntity, TResult>> result);

        /// <summary>
        /// 查询数量
        /// </summary>
        /// <returns></returns>
        Task<int> QueryCount(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 查询是否存在
        /// </summary>
        /// <returns></returns>
        Task<bool> QueryExist(Expression<Func<TEntity, bool>> expression);
    }
}
